package com.vipheyue.educationmp4.activity;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.universalvideoview.UniversalMediaController;
import com.universalvideoview.UniversalVideoView;
import com.vipheyue.educationmp4.R;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CourseDisplayActivity extends AppCompatActivity implements UniversalVideoView.VideoViewCallback {

    private static final String TAG = "CourseDisplayActivity";
    private static final String SEEK_POSITION_KEY = "SEEK_POSITION_KEY";
    //    private   String VIDEO_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
    private String VIDEO_URL;

    UniversalVideoView mVideoView;
    UniversalMediaController mMediaController;

    View mBottomLayout;
    View mVideoLayout;
    TextView mStart;

    @Bind(R.id.videoView)
    UniversalVideoView videoView;
    @Bind(R.id.media_controller)
    UniversalMediaController media_controller;
    @Bind(R.id.video_layout)
    FrameLayout video_layout;
    @Bind(R.id.start)
    Button start;

    @Bind(R.id.mideo_back)
    Button mideo_back;
    @Bind(R.id.forward)
    Button forward;
    @Bind(R.id.btn_cycle)
    Button btn_cycle;
    @Bind(R.id.bottom_layout)
    LinearLayout bottom_layout;

    private int mSeekPosition;
    private int cachedHeight;
    private boolean isFullscreen;
    private boolean canCycler = true;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display);
        ButterKnife.bind(this);
        initView();
        mVideoLayout = findViewById(R.id.video_layout);
        mBottomLayout = findViewById(R.id.bottom_layout);
        mVideoView = (UniversalVideoView) findViewById(R.id.videoView);
        mMediaController = (UniversalMediaController) findViewById(R.id.media_controller);
        mVideoView.setMediaController(mMediaController);
        setVideoAreaSize();
        mVideoView.setVideoViewCallback(this);

        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                Log.d(TAG, "onCompletion ");
                if (canCycler) {
//                    mVideoView.start();
                    media_controller.replay();
                }
            }
        });
        startMovie();
        start.setText("暂停");
    }

    private void initView() {
        VIDEO_URL = getIntent().getStringExtra("uri");

        com.vipheyue.fastlib.utils.Log.e(VIDEO_URL);

        if (canCycler) {
            btn_cycle.setText("停止循环播放");
        } else {
            btn_cycle.setText("开始循环播放");
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        Log.d(TAG, "onPause ");
        if (mVideoView != null && mVideoView.isPlaying()) {
            mSeekPosition = mVideoView.getCurrentPosition();
            Log.d(TAG, "onPause mSeekPosition=" + mSeekPosition);
            mVideoView.pause();
        }
    }

    /**
     * 置视频区域大小
     */
    private void setVideoAreaSize() {
        mVideoLayout.post(new Runnable() {
            @Override
            public void run() {
                int width = mVideoLayout.getWidth();
                cachedHeight = (int) (width * 405f / 720f);
//                cachedHeight = (int) (width * 3f / 4f);
//                cachedHeight = (int) (width * 9f / 16f);
                ViewGroup.LayoutParams videoLayoutParams = mVideoLayout.getLayoutParams();
                videoLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
                videoLayoutParams.height = cachedHeight;
                mVideoLayout.setLayoutParams(videoLayoutParams);
                mVideoView.setVideoPath(VIDEO_URL);
                mVideoView.requestFocus();
            }
        });
    }


    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.d(TAG, "onSaveInstanceState Position=" + mVideoView.getCurrentPosition());
        outState.putInt(SEEK_POSITION_KEY, mSeekPosition);
    }

    @Override
    protected void onRestoreInstanceState(Bundle outState) {
        super.onRestoreInstanceState(outState);
        mSeekPosition = outState.getInt(SEEK_POSITION_KEY);
        Log.d(TAG, "onRestoreInstanceState Position=" + mSeekPosition);
    }


    @Override
    public void onScaleChange(boolean isFullscreen) {
        this.isFullscreen = isFullscreen;
        if (isFullscreen) {
            ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
            mVideoLayout.setLayoutParams(layoutParams);
            mBottomLayout.setVisibility(View.GONE);

        } else {
            ViewGroup.LayoutParams layoutParams = mVideoLayout.getLayoutParams();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = this.cachedHeight;
            mVideoLayout.setLayoutParams(layoutParams);
            mBottomLayout.setVisibility(View.VISIBLE);
        }
        switchTitleBar(!isFullscreen);
    }

    private void switchTitleBar(boolean show) {
        ActionBar supportActionBar = getSupportActionBar();
        if (supportActionBar != null) {
            if (show) {
                supportActionBar.show();
            } else {
                supportActionBar.hide();
            }
        }
    }

    @Override
    public void onPause(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onPause UniversalVideoView callback");
        start.setText("开始");
    }

    @Override
    public void onStart(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onStart UniversalVideoView callback");
        start.setText("暂停");
    }

    @Override
    public void onBufferingStart(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onBufferingStart UniversalVideoView callback");
    }

    @Override
    public void onBufferingEnd(MediaPlayer mediaPlayer) {
        Log.d(TAG, "onBufferingEnd UniversalVideoView callback");
    }

    @Override
    public void onBackPressed() {
        if (this.isFullscreen) {
            mVideoView.setFullscreen(false);
        } else {
            super.onBackPressed();
        }
    }

    @OnClick({R.id.start, R.id.mideo_back, R.id.forward, R.id.btn_cycle})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.start:
//                startMovie();
                paruseMovie();
                break;
            case R.id.mideo_back:
                backMovie();
                break;
            case R.id.forward:
                forwardMovie();
                break;
            case R.id.btn_cycle:
                canCycler = !canCycler;
                if (canCycler) {
                    btn_cycle.setText("停止循环播放");
                } else {
                    btn_cycle.setText("开始循环播放");
                }
                break;
        }
    }

    private void paruseMovie() {
        if (mVideoView != null) {
            if (mVideoView.isPlaying()) {
                mSeekPosition = mVideoView.getCurrentPosition();
                mVideoView.pause();
            } else {
                mVideoView.start();
            }

        }
    }

    private void forwardMovie() {
        mVideoView.start();
        mSeekPosition = mVideoView.getCurrentPosition();
        if (mSeekPosition > 0) {
            mVideoView.seekTo(mSeekPosition + 4000);
        }
    }

    private void backMovie() {
        mVideoView.start();
        mSeekPosition = mVideoView.getCurrentPosition();
        if (mSeekPosition > 0) {
            mVideoView.seekTo(mSeekPosition - 4000);
        }
    }


    private void startMovie() {
        if (mSeekPosition > 0) {
            mVideoView.seekTo(mSeekPosition);
//                    mSeekPosition = mVideoView.getCurrentPosition();
//                    mVideoView.seekTo(mSeekPosition - 4000);
        }
        mVideoView.start();
        mMediaController.setTitle("视频播放");
    }
}

